<?php

require_once "config.php";
require_once "weixin_info.php";

$u_id=$_SESSION['Member_info'][0]['id'];//25;
// print_r($_SESSION);exit;
$user = $mysql->query("select * from dyspw_member where id=$u_id");
$user_rows = $user->fetch_all(MYSQLI_ASSOC);

$user_name = $user_rows[0]['username'];
$user_img = $user_rows[0]['avatar'];

$aid = $_POST['aid'];
$txt = $_POST['txt'];
$now_time = time();

$web_type = $mysql->query("select * from dyspw_weblinktype left join dyspw_weblink on dyspw_weblink.classid = dyspw_weblinktype.id where dyspw_weblink.id={$aid}");
$web_type_rows = $web_type->fetch_all(MYSQLI_ASSOC);

$add = $mysql->query("insert into dyspw_usercomment (aid,molds,uid,uname,body,time,uimage) values ('{$aid}','{$web_type_rows[0]['classname']}','{$u_id}','{$user_name}','{$txt}','{$now_time}','{$user_img}')");

if($add){
    $commentsql = $mysql->query("select * from dyspw_usercomment where aid={$aid} and body='{$txt}'"); //and uid={$u_id} and body={$txt} and time={$now_time}");
    $comment_rows= $commentsql->fetch_all(MYSQLI_ASSOC);
    $conment_sum = count($comment_rows);

    //评论回复
    if(empty($comment_rows))
    {
        $m_c_rows['conment'] = '';
    }else{

        foreach ($comment_rows as $kk=>$vv)
        {
            $m_c_rows['conment'][$kk] = $vv;
            $conmentlike_sql = $mysql->query("select * from dyspw_conment_like where u_id={$u_id} and conment_id='{$vv['id']}'");
            $conmentlike_rows = $conmentlike_sql->fetch_all(MYSQLI_ASSOC);

            if(empty($conmentlike_rows)){
                $m_c_rows['conment'][$kk]['like_state'] = 0;
            }else{
                $m_c_rows['conment'][$kk]['like_state'] = $conmentlike_rows[0]['state'];
            }

            $user_message = $mysql->query("select * from dyspw_message where c_id={$vv['id']}");
            $message_rows = $user_message->fetch_all(MYSQLI_ASSOC);
            if(empty($message_rows))
            {
                $m_c_rows['conment'][$kk]['message'] ='';
            }else{
                foreach ($message_rows as $m_k=>$m_v)
                {
                    $m_c_rows['conment'][$kk]['message'][$m_k] = $m_v;

                }
                $m_c_rows['conment'][$kk]['message_sum'] = count($message_rows);
            }
        }


    }
    echo json_encode(['code'=>1,'rows'=>$m_c_rows['conment'][0]]);
}else{
    print_r("失败");
}


?>
